﻿using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.Extensions.DependencyInjection;
using System;
using WebApiV3.Data;
using WebApiV3.Data.Entity;
using WebApiV3.Domain;

namespace WebApiV3.Utils
{
    public class DbInitializer
    {
        public static void Seed (IServiceProvider serviceProvider)
        {
            using (var scope = serviceProvider.CreateScope())
            {
                var db = scope.ServiceProvider.GetService(typeof(Admin3200ConText)) as Admin3200ConText;
                var _user = scope.ServiceProvider.GetService(typeof(IRespository<Users>)) as EfRespository<Users>;
                var _role = scope.ServiceProvider.GetService(typeof(IRespository<Roles>)) as EfRespository<Roles>;

                db.Database.EnsureCreated();

                var hasUser = db.Users.Any();

                if (!hasUser)
                {
                    var role = new Roles()
                    {
                        RoleName = "导演"
                    };
                    _role.Insert(role);

                    var user = new Users()
                    {
                        UserName = "LXueling",
                        Password = "19835604983",
                        Roles = role
                    };
                    _user.Insert(user);
                }
  
            }
            
        }
    }
}
